// pages/subcribe_detail/subcribe_detail.js
var app = getApp();
const api = require('../../utils/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dates: [{xinqi:'日', date: ''},{xinqi:'一', date: ''}, {xinqi:'二', date: ''}, {xinqi:'三', date: ''},
    {xinqi:'四', date: ''}, {xinqi:'五', date: ''}, {xinqi:'六', date: ''}
  ], 
    select_index: 0, 
    dialog: false, 
    dialog_time_select_index: 1,
    doctor: [], 
    pet_list: [],
    pet_list_name: [], 
    pet_index: 0, 
    pet_cur_id: null, 
    section_id: null,
    doctor_id: null,
    showPayPwdInput: false,  //是否展示密码输入层
    pwdVal: '',  //输入的密码
    payFocus: true, //文本框焦点
  },
  submitReg(){
    var reg = {startTime: null,sectionId: this.data.section_id, money: 12, doctorId: this.data.doctor_id, petId: this.data.pet_cur_id}
    console.debug('reg:')
    reg.startTime = '2021-'+this.data.dates[this.data.select_index].date
    // reg.startTime = '2021-06-05';
    console.debug(reg)
    api.myPostBody('/reg', reg).then(res => {
      console.debug(res)
      this.setData({dialog: false})
    }).catch(err => {
      console.debug(err)
    })
  },
  sendRequest : function(){
    var that=this;//获取到全局变量
    wx.request({
      url: app.globalData.url+'/doctor/all',//给函数传递服务器地址参数 
      data: {
        type: 'only_doc'
      },//给服务器传递数据，本次请求不需要数据，可以不填
      header: {
        // 'content-type': 'application/json' // 默认值，返回的数据设置为json数组格式
      },
      success: function (res) {
        console.log(res.data.res);//打印出返回的数据
        that.setData({
          doctor: res.data.res
        })//通过setData方法把返回的数据复制给本页面定义的list数组
      },
    })
  },

  selectDialogTime(e){
    var index = e.currentTarget.dataset.index
    this.setData({dialog_time_select_index: index})

    console.debug('doctor index: '+index)

    //设置doctor_id
    this.setData({doctor_id: this.data.doctor[index].id})
  },

  selectData(e){
    var index = e.currentTarget.dataset.index
    this.setData({select_index: index})
  },
  showDialog(e){
    var doctor_id = e.currentTarget.dataset.doctor_id
    this.setData({dialog: this.data.dialog?false:true})
    this.setData({doctor_id: doctor_id})
  },
    /**
   * 显示支付密码输入层
   */
  showInputLayer: function(){
    if(this.data.pet_list_name == null || this.data.pet_list_name.length == 0){
      wx.showToast({
        title: "必须先添加宠物后才能挂号",
      })
    }else {
      this.setData({ showPayPwdInput: true, payFocus: true });
    }
    
  },
  
  /**
   * 隐藏支付密码输入层
   */
  hidePayLayer: function(){
    
    var val = this.data.pwdVal;

    this.setData({ showPayPwdInput: false, payFocus: false, pwdVal: '' });
    if(val.length >= 6){
      wx.showToast({
        title: "支付成功",
      })
      this.submitReg()
    }
    
  },
  /**
   * 获取焦点
   */
  getFocus: function(){
    this.setData({ payFocus: true });
  },
  /**
   * 输入密码监听
   */
  inputPwd: function(e){
      this.setData({ pwdVal: e.detail.value });

      if (e.detail.value.length >= 6){
        this.hidePayLayer();
      }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.showInputLayer();
    var that = this
    //设置section_id
    console.debug('onload potion:'+JSON.stringify(options))
    that.setData({section_id: options.section_id})



    this.sendRequest();
    //获取我的宠物列表信息
    api.myGet('/pet/all', null).then(res => {
      if(res.data.res != null && res.data.res.length > 0){
        //设置pet_cur_id
        this.setData({pet_cur_id: res.data.res[0].id})
        this.setData({pet_list: res.data.res}) 
        var names = this.data.pet_list_name
        for (var i in this.data.pet_list) {
          names.push(this.data.pet_list[i].nickname)
        }
        this.setData({pet_list_name: names})
        console.debug(this.data.pet_list_name)
      }
    }).catch(err => {
    })
  },

  pickerChange(e){
    console.debug('pickerChange')
    console.debug(e)
    this.setData({pet_index: e.detail.value})
    this.setData({pet_cur_id: this.data.pet_list[e.detail.value].id})
    console.debug('pet cur id:'+this.data.pet_cur_id)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    //设置星期数
    var nows = new Date();
    that.setData({select_index: nows.getDay()});
    var curDay = nows.getDay();
    console.debug('days:',that.data.dates)
    for(var i = 0; i<that.data.dates.length;i++){
      var now = new Date();
      var span = i - curDay;
      now.setDate(now.getDate()+span)
      var value = now.getMonth()+1 + '-'+now.getDate();
      that.data.dates[i].date = value;
      console.debug('value+'+span, value)
      that.setData({dates: that.data.dates})
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})